package com.andreabaccega.simplegps;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.location.GpsSatellite;
import android.location.LocationManager;
import android.util.AttributeSet;

import android.util.Log;
import android.view.View;

public class GpsView extends View
{
	private int padding=20;
	private Paint verde,black,sat_used,giallo,sat_unused;
	private LocationManager lm ;
	private float rotazioneGrafico= 0;
	private Bitmap compass;
	private float width,height;
	public GpsView(Context context, AttributeSet attrs) {
		// inizializzo le varie Palette
		super(context, attrs);


		verde = new Paint();
		verde.setColor(Color.GREEN);
		verde.setAntiAlias(true);
		verde.setTextSize(15);

		sat_unused = new Paint();
		sat_unused.setColor(Color.RED);
		sat_unused.setAntiAlias(true);
		sat_unused.setTextSize(16);
		sat_unused.setTypeface(Typeface.MONOSPACE);

		giallo = new Paint();
		giallo.setColor(Color.YELLOW);
		giallo.setAntiAlias(true);
		giallo.setTextSize(17);
		giallo.setTypeface(Typeface.MONOSPACE);

		black = new Paint();
		black.setColor(Color.BLACK);
		black.setAntiAlias(true);

		sat_used = new Paint(); 
		sat_used.setColor(Color.YELLOW);
		sat_used.setTextSize(16);
		sat_used.setTypeface(Typeface.MONOSPACE);
		lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		
		compass = BitmapFactory.decodeResource(context.getResources(), R.drawable.compass800);
		
	}
	public void newAzimuth(float newVal) {
		rotazioneGrafico = -newVal;
		this.invalidate();
	}
	@Override
	public void onDraw (Canvas canvas) {
		width = getWidth();
		height = getHeight();
		padding = this.getPaddingBottom();
		canvas.rotate(rotazioneGrafico, width/2, height/2);
		canvas.drawBitmap(compass, null, 
				new Rect(
						(int)(width/2 - ((width<height?width/2:height/2) )+padding),
						(int)(height/2 -((width<height?width/2:height/2) )+padding ),
						(int)(width/2 + ((width<height?width/2:height/2) )-padding),
						(int)(height/2 + (width<height?width/2:height/2) -padding)), verde);
		/*
		// Cerchio esterno
		canvas.drawCircle(width/2, height/2, (width<height?width/2:height/2)-padding, verde);

		// Cerchio interno
		canvas.drawCircle(width/2, height/2, (width<height?width/2:height/2) -padding- 5, black);
	
		//Scrivo l'incrocio.
		canvas.drawLine(width/2 - ((width<height?width/2:height/2) )+padding+ 5 ,
				height/2 ,
				width/2+  (width<height?width/2:height/2) -padding- 5 ,
				height/2, 
				verde);
		canvas.drawLine(width/2,
				height/2 - ((width<height?width/2:height/2) )+padding+ 5 ,
				width/2,
				height/2 + (width<height?width/2:height/2) -padding- 5 ,
				verde);
		/*
		// MEtto i punti cardinali
		canvas.drawText("N", width/2 - giallo.getTextSize()/3, height/2 -((width<height?width/2:height/2) )+padding - 5, giallo);
		canvas.drawText("S", width/2 - giallo.getTextSize()/3, height/2 + (width<height?width/2:height/2) -padding +giallo.getTextSize(), giallo);

		canvas.drawText("W", width/2 - ((width<height?width/2:height/2) )+padding - giallo.getTextSize(), height/2+giallo.getTextSize()/3, giallo);
		canvas.drawText("E", width/2 + ((width<height?width/2:height/2) )-padding + 5, height/2+giallo.getTextSize()/3, giallo);
		
		*/
		 

		// Inserisco satelliti :P
		try {

			for  (GpsSatellite sat: lm.getGpsStatus(null).getSatellites()){
				double tmp[] = getCoordinates(width/2, 
					 	height/2, 
						((width<height?width/2:height/2) )-padding , 
						sat.getAzimuth(), 
						sat.getElevation());
				String prn = Integer.toString(sat.getPrn());
				tmp[0]= tmp[0]-sat_used.getTextSize()/2*prn.length();
				tmp[1]= tmp[1]+sat_used.getTextSize()/2;
				if (sat.usedInFix()) {
					canvas.drawRect(
							(float) tmp[0]-4, 
							(float) tmp[1]-sat_used.getTextSize()/2-8, 
							(float) tmp[0]+sat_used.getTextSize()/2*prn.length() +6,
							(float) tmp[1]+4, 
							sat_used
					);
				} else {
					canvas.drawRect(
							(float) tmp[0]-4, 
							(float) tmp[1]-sat_unused.getTextSize()/2-8, 
							(float) tmp[0]+sat_unused.getTextSize()/2*prn.length() +6,
							(float) tmp[1]+4, 
							sat_unused
					);
				}
				canvas.drawRect(
						(float) tmp[0]-2, 
						(float) tmp[1]-sat_used.getTextSize()/2-6, 
						(float) tmp[0]+sat_used.getTextSize()/2*prn.length() +4,
						(float) tmp[1]+2, 
						black
				);

				canvas.drawText(prn, 
						(float) tmp[0], 
						(float) tmp[1], sat_used);
			}
		} catch (NullPointerException ex){
			Log.d("LOL","EXC");
		};
	}



	/**
	 * 
	 * @param centroX
	 * @param centroY
	 * @param raggio
	 * @param seno
	 * @param coseno
	 * @return
	 */
	private static double [] getCoordinates(double centroX , double centroY, double raggio, double azimuth, double elevation) {
		double [] toRet = new double[2];
		toRet[1] = ( Math.sin(Math.toRadians(-90 + azimuth)) * raggio) * Math.cos(Math.toRadians(elevation)) + centroY;
		toRet[0] = ( Math.cos(Math.toRadians(-90 + azimuth)) * raggio ) * Math.cos(Math.toRadians(elevation))+ centroX;
		return toRet;
	}
}
